package org.crystalrange.files.web;

import org.apache.commons.lang3.StringUtils;
import org.crystalrange.files.model.FileType;
import org.crystalrange.searching.SearchOperatorListitemRenderer;
import org.crystalrange.searching.SearchOperators;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.util.GenericForwardComposer;
import org.zkoss.zkplus.databind.AnnotateDataBinder;
import org.zkoss.zul.Bandbox;
import org.zkoss.zul.ListModelList;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.Textbox;
import org.zkoss.zul.Window;

import com.googlecode.genericdao.search.Filter;
import com.googlecode.genericdao.search.Search;

public class FileSearchController extends GenericForwardComposer {

	protected Listbox sortOperatorName;
	protected Listbox sortOperatorDescription;
	protected Listbox sortOperatorFileType;
	protected Search searchFile;
	protected Textbox textboxName;
	protected Textbox textboxDescription;
	protected Bandbox bandboxFileType;
	protected FileType selectedFileType;
	protected AnnotateDataBinder binder;

	public Search getSearchFile() {
		return searchFile;
	}

	public void setSearchFile(Search searchFile) {
		this.searchFile = searchFile;
	}
	
	public FileType getSelectedFileType() {
		return selectedFileType;
	}

	public void setSelectedFileType(FileType selectedFileType) {
		this.selectedFileType = selectedFileType;
	}

	@Override
	public void doAfterCompose(Component component) throws Exception {
		super.doAfterCompose(component);

		this.sortOperatorName.setModel(new ListModelList(SearchOperators
				.getAllOperators()));
		this.sortOperatorName
				.setItemRenderer(new SearchOperatorListitemRenderer());

		this.sortOperatorDescription.setModel(new ListModelList(SearchOperators
				.getAllOperators()));
		this.sortOperatorDescription
				.setItemRenderer(new SearchOperatorListitemRenderer());

		this.sortOperatorFileType.setModel(new ListModelList(SearchOperators
				.getAllOperators()));
		this.sortOperatorFileType
				.setItemRenderer(new SearchOperatorListitemRenderer());

		this.searchFile = (Search) this.execution.getAttribute("searchFile");

		if (this.searchFile != null) {
			for (Filter filter : this.searchFile.getFilters()) {
				if (filter.getProperty().equals("name")) {
					this.restoreOperator(this.sortOperatorName, filter);
					this.textboxName.setValue(filter.getValue().toString());
				} else if (filter.getProperty().equals("description")) {
					this.restoreOperator(this.sortOperatorDescription, filter);
					this.textboxDescription.setValue(filter.getValue()
							.toString());
				} else if (filter.getProperty().equals("fileType")) {
					this.restoreOperator(this.sortOperatorFileType, filter);
					this.selectedFileType = (FileType) filter.getValue();
				}
			}
		}

		component.setAttribute("controller", this);
	}

	private void restoreOperator(Listbox listbox, Filter filter) {
		if (filter.getOperator() == Filter.OP_EQUAL) {
			listbox.setSelectedIndex(1);
		} else if (filter.getOperator() == Filter.OP_NOT_EQUAL) {
			listbox.setSelectedIndex(2);
		} else if (filter.getOperator() == Filter.OP_LESS_THAN) {
			listbox.setSelectedIndex(3);
		} else if (filter.getOperator() == Filter.OP_GREATER_THAN) {
			listbox.setSelectedIndex(4);
		} else if (filter.getOperator() == Filter.OP_LESS_OR_EQUAL) {
			listbox.setSelectedIndex(5);
		} else if (filter.getOperator() == Filter.OP_GREATER_OR_EQUAL) {
			listbox.setSelectedIndex(6);
		} else if (filter.getOperator() == Filter.OP_ILIKE) {
			listbox.setSelectedIndex(7);
		}
	}

	public void onClick$buttonOk(Event event) throws Exception {
		this.searchFile.getFilters().clear();

		if (StringUtils.isNotEmpty(this.textboxName.getText())) {
			Listitem item = this.sortOperatorName.getSelectedItem();
			if (item != null) {
				SearchOperators searchOperator = (SearchOperators) item
						.getValue();
				if (searchOperator.getSearchOperatorId() != -1) {
					this.searchFile.addFilter(new Filter("name",
							this.textboxName.getValue(), searchOperator
									.getSearchOperatorId()));
				}
			}
		}

		if (StringUtils.isNotEmpty(this.textboxDescription.getText())) {
			Listitem item = this.sortOperatorDescription.getSelectedItem();
			if (item != null) {
				SearchOperators searchOperator = (SearchOperators) item
						.getValue();
				if (searchOperator.getSearchOperatorId() != -1) {
					this.searchFile.addFilter(new Filter("description",
							this.textboxDescription.getValue(), searchOperator
									.getSearchOperatorId()));
				}
			}
		}

		if (StringUtils.isNotEmpty(this.bandboxFileType.getText())) {
			Listitem item = this.sortOperatorFileType.getSelectedItem();
			if (item != null) {
				SearchOperators searchOperator = (SearchOperators) item
						.getValue();
				if (searchOperator.getSearchOperatorId() != -1) {
					this.searchFile.addFilter(new Filter("fileType",
							this.selectedFileType, searchOperator
									.getSearchOperatorId()));
				}
			}
		}

		this.execution.setAttribute("searchFile", this.searchFile);
		this.execution.setAttribute("result", "ok");
		this.self.detach();
	}

	public void onClick$buttonCancel(Event event) throws Exception {
		this.execution.setAttribute("searchFile", null);
		this.execution.setAttribute("result", "cancel");
		this.self.detach();
	}

	public void onOpen$bandboxFileType(Event event) throws Exception {
		Window window = (Window) this.execution.createComponents(
				"~./zul/html/fileTypeSelect.zul", null, null);
		window.setParent(self);
		window.doModal();

		if (this.execution.getAttribute("result") == "ok") {
			this.selectedFileType = (FileType) this.execution
					.getAttribute("selectedFileType");
		}

		this.binder.loadAll();
	}

}
